﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;

namespace BestBuy.PageAdmin
{
    public partial class PageAddCatalog : System.Web.UI.Page
    {
        private CDanhMuc _cdm = new CDanhMuc();

        protected void Page_Load(object sender, EventArgs e)
        {
            lbThongBao.Visible = false;
            if (!IsPostBack)
            {
                LoadCombobox();
            }

        }

        private void LoadCombobox()
        {
            List<DanhMuc> list = _cdm.GetListCataLogAddParent();
            if (list.Count > 0)
            {
                cbParent.DataSource = list;
                cbParent.DataTextField = "TenDanhMuc";
                cbParent.DataValueField = "IDDanhMuc";
                cbParent.DataBind();
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            lbThongBao.Visible = true;
            if (txtName.Text.Equals("")) {
                pnThongBao.CssClass = "InRight error message";
                lbThongBao.Text = "Name isn't emtry.";
                return;
            }
            DanhMuc dm = new DanhMuc();
            dm.TenDanhMuc = txtName.Text.Trim();
            if (cbParent.SelectedIndex > 0)
            {
                dm.IDDanhMucCha = Convert.ToInt32(cbParent.SelectedValue.ToString());
            }
            dm.TinhTrang = true;
            bool kq = _cdm.AddCatalog(dm);
            if (kq)
            {
                pnThongBao.CssClass = "InRight success message";
                lbThongBao.Text = "Add new category success.";
            }
            else
            {
                pnThongBao.CssClass = "InRight error message";
                lbThongBao.Text = "Add new category failure.";
            }
        }


    }
}